package cn.iocoder.yudao.module.blog.service.spacelike;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.spacelike.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.spacelike.BlogSpaceLikeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.spacelike.BlogSpaceLikeConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.spacelike.BlogSpaceLikeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 动态点赞 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogSpaceLikeServiceImpl implements BlogSpaceLikeService {

    @Resource
    private BlogSpaceLikeMapper spaceLikeMapper;

    @Override
    public Long createSpaceLike(BlogSpaceLikeCreateReqVO createReqVO) {
        // 插入
        BlogSpaceLikeDO spaceLike = BlogSpaceLikeConvert.INSTANCE.convert(createReqVO);
        spaceLikeMapper.insert(spaceLike);
        // 返回
        return spaceLike.getId();
    }

    @Override
    public void updateSpaceLike(BlogSpaceLikeUpdateReqVO updateReqVO) {
        // 校验存在
        validateSpaceLikeExists(updateReqVO.getId());
        // 更新
        BlogSpaceLikeDO updateObj = BlogSpaceLikeConvert.INSTANCE.convert(updateReqVO);
        spaceLikeMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpaceLike(Long id) {
        // 校验存在
        validateSpaceLikeExists(id);
        // 删除
        spaceLikeMapper.deleteById(id);
    }

    private void validateSpaceLikeExists(Long id) {
        if (spaceLikeMapper.selectById(id) == null) {
            throw exception(SPACE_LIKE_NOT_EXISTS);
        }
    }

    @Override
    public BlogSpaceLikeDO getSpaceLike(Long id) {
        return spaceLikeMapper.selectById(id);
    }

    @Override
    public List<BlogSpaceLikeDO> getSpaceLikeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return spaceLikeMapper.selectBatchIds(ids);
    }

    @Override
    public List<BlogSpaceLikeDO> getSpaceLikeList(Long memberId, Collection<Long> spaceIds) {
        if(CollUtil.isEmpty(spaceIds)){
            return ListUtil.empty();
        }
        List<BlogSpaceLikeDO> list = spaceLikeMapper.selectList(BlogSpaceLikeDO::getSpaceId, spaceIds);

        if(list.size() == 0){
            return list;
        }

        List<BlogSpaceLikeDO> list2 = new ArrayList<>();
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).getMemberId().equals(memberId)){
                list2.add(list.get(i));
            }
        }

        return list2;
    }

    @Override
    public PageResult<BlogSpaceLikeDO> getSpaceLikePage(BlogSpaceLikePageReqVO pageReqVO) {
        return spaceLikeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogSpaceLikeDO> getSpaceLikeList(BlogSpaceLikeExportReqVO exportReqVO) {
        return spaceLikeMapper.selectList(exportReqVO);
    }

}
